package gui.components.JXTable.JXCell;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.UIResource;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.util.HashMap;

/**
 * User: Programmer
 * Date: 10.05.2006
 * Time: 13:49:15
 */
public class JXBooleanCellRenderer extends JCheckBox implements TableCellRenderer, UIResource, iJXCellRenderer{
    private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
    private boolean selDrawing = true;

    private Color defaultCellColor = new Color(255,255,255);
    private Color defaultFontColor = new Color(0,0,0);
    private Color selectedColor = new Color(184,207,229);
    private Border selBorder = UIManager.getBorder("Table.focusCellHighlightBorder");

    private Color defaultColumnColor = new Color(255,255,255);
    private Color defaultRowColor = new Color(255,128,255);

    private Font defaultFont = new JTable().getFont();
    private Font defaultNullFont = new Font("Arial",0,0);

    private HashMap<Integer, HashMap<Integer,Color>> colorCells = new HashMap<Integer, HashMap<Integer, Color>>();
    private HashMap<Integer,Color> colorRows = new HashMap<Integer, Color>();
    private HashMap<Integer,Color> colorColumns = new HashMap<Integer, Color>();

    public void addColorColumn(Integer key, Color color){
        colorColumns.put(key,color);
    }

    public void addColorColumn(Integer key){
        addColorColumn(key,defaultColumnColor);
    }

    public void removeColorColumn(Integer key){
        colorColumns.remove(key);
    }

    public void addColorRow(Integer key, Color color){
        colorRows.put(key,color);
    }

    public void removeAllColorRow() {
        colorRows.clear();
    }

    public void addColorRow(Integer key){
        addColorRow(key,defaultRowColor);
    }

    public void removeColorRow(Integer key){
        colorRows.remove(key);
    }

    public void addColorCell(Integer rowkey, Integer column, Color color){
        if(!colorCells.containsKey(rowkey)){
            HashMap<Integer,Color> tmp = new HashMap<Integer, Color>();
            tmp.put(column,color);
            colorCells.put(rowkey,tmp);
        }else{
            colorCells.get(rowkey).put(column,color);
        }
    }

    public void addColorCell(Integer rowkey, Integer column){
        addColorCell(rowkey,column,defaultCellColor);
    }

    public void removeColorCell(Integer rowkey, Integer column){
        if(colorCells.containsKey(rowkey)){
            colorCells.get(rowkey).remove(column);
        }
    }

    public void setSelectionDrawing(boolean b){
        selDrawing = b;
    }


    public JXBooleanCellRenderer() {
        super();
        setHorizontalAlignment(JLabel.CENTER);
        setBorderPainted(true);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if(column==0)return this;
        this.setOpaque(true);
        //setForeground(table.getForeground());
        setBackground(table.getBackground());
        if (colorColumns.containsKey(column)){setBackground(colorColumns.get(column));}
        if (colorRows.containsKey(row)){setBackground(colorRows.get(row));}
        if (colorCells.containsKey(row)){
            if(colorCells.get(row).containsKey(column)){
                setBackground(colorCells.get(row).get(column));
            }
        }
        if (isSelected) {
            if(selDrawing){
                //setForeground(table.getSelectionForeground());
                super.setBackground(table.getSelectionBackground());
            }
        }

        setSelected((value != null && ((Boolean)value).booleanValue()));


        if (hasFocus) {
            setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
        } else {
            setBorder(noFocusBorder);
        }

        return this;
    }
}

